/***************** Modification History ***********************************

   @doc
   @module Confio.c - Contains the generic table load and save functions |
   @end
   
$Log: /SoftPay/SPK2SIM/Source/Config/confio.c $ 
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#include <project.h>


#include <aclconio.h>
#include <stdio.h>
#include <stdio.h>
#include <string.h>

/** /#define DEBUG_VSCONFIO*/
#ifdef DEBUG_VSCONFIO
#define DEBUG_95
#endif

#include <define.h>
#include <spdebug.h>
#include "Isosizes.h"
#include "Iso8bmp.h"
#include <confio.h>
#include <file.h>

/*-------------------------------------------------------------------------
    Function:       inLoadConfRec
    Description:    Loads a record from a given table
    Parameters:     char *, int, int, char *
    Returns:        VS_ERR, VS_SUCCESS
    Notes:
--------------------------------------------------------------------------*/
int inLoadConfRec(const char* szFileName,int inRecSize,int inRecNum,char* pchConfRec) {
    int inResult;
    FHANDLE hConfFHandle;
    long lnSeekResult,
    lnOffset;

    /**/ pdebug(("--inLoadConfRec--"));/**/
    /**/ pdebug(("szFileName = %s", szFileName));/**/

    hConfFHandle = open(szFileName, O_RDWR);

    if (hConfFHandle < 0) {
        /**/ pdebug(("Could not open file"));/**/
        return(VS_ERR);
    }

    lnOffset = (long) (((long) inRecNum * (long) inRecSize) + GEN_VER_SIZE);
    lnSeekResult = lseek(hConfFHandle, lnOffset, SEEK_SET);

    if (lnSeekResult != lnOffset)  /* Invalid index given, return VS_ERR */ {
        /**/ pdebug(("Invalid index given"));/**/
        close(hConfFHandle);
        return(VS_ERR);
    }

    inResult = read(hConfFHandle, pchConfRec, inRecSize);
    if (inResult == 0L)          /* End of file reached, invalid index  */ {
        /**/ pdebug(("End of file reached"));/**/
        close(hConfFHandle);
        return(VS_ERR);
    }

    if (close(hConfFHandle) < 0) {
        /**/ pdebug(("End of file reached"));/**/
        return(VS_ERR);
    }       

    return(VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function:       inSaveConfRec
    Description:    Saves a record from a given table
    Parameters:     char *, int, int, char *
    Returns:        VS_ERR, VS_SUCCESS
    Notes:
--------------------------------------------------------------------------*/

int inSaveConfRec(const char* szFileName,int inRecSize,int inRecNum,char* pchConfRec) {
    int inResult;
    FHANDLE hConfFHandle;
    long lnOffset,
    lnSeekResult;

    /** pdebug(("--inSaveConfRec--"));*/
    /** pdebug(("szFileName = %s", szFileName));*/

    hConfFHandle = open(szFileName, O_RDWR);
    if (hConfFHandle < 0)
        return(VS_ERR);

    lnOffset = (long) (((long) inRecNum * (long) inRecSize) + GEN_VER_SIZE);
    lnSeekResult = lseek(hConfFHandle, lnOffset, SEEK_SET);
    if (lnSeekResult != lnOffset)   /* Invalid index given, return VS_ERR */ {
        close(hConfFHandle);
        return(VS_ERR);
    }

    inResult = write(hConfFHandle, pchConfRec, inRecSize);
    if (inResult != inRecSize)              /* Error writing record info */ {
        close(hConfFHandle);
        return(VS_ERR);
    }

    if (close(hConfFHandle) < 0)
        return(VS_ERR);
    return(VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    @func int | inGetNumberOfConfRecs | Gets number of <p inRecSize> sized records within the Gendata file <p szFileName>
    @parm char * | szFileName | Name of file to query
    @parm int | inRecSize | Size of each record in file
    @rdesc Returns the number of records in the file
    @end
--------------------------------------------------------------------------*/
int inGetNumberOfConfRecs(char* szFileName,int inRecSize) {
    long lnSize;

    lnSize = lnGetFileSize(szFileName);
    return((int) ((lnSize - GEN_VER_SIZE) / inRecSize));
}

/*-------------------------------------------------------------------------
    @func int | inAppendConfRec | Saves contents of specified record to end of file
    @parm char * | szFileName | Name of file to append to
    @parm int | inRecSize | Size of each record in file
    @parm char * | pchConfRec | Pointer to location to read the record data from
    @rdesc Returns VS_SUCCESS of VS_ERR
    @end
--------------------------------------------------------------------------*/

int inAppendConfRec(char* szFileName,int inRecSize,char* pchConfRec) {
    int inResult = VS_ERR;
    FHANDLE hConfFHandle;

    if ((hConfFHandle = open(szFileName, O_RDWR | O_CREAT)) >= 0) {
        /* Seek to the end of the file */
        if (lseek(hConfFHandle, 0L, SEEK_END) >= 0)
            if (write(hConfFHandle, pchConfRec, inRecSize) == inRecSize)
                inResult = VS_SUCCESS;
        close(hConfFHandle);
    }
    return(inResult);
}

/*-------------------------------------------------------------------------
    @func int | inDeleteConfRec | Deletes record from specified file
    @parm char * | szFileName | Name of file to delete to from
    @parm int | inRecSize | Size of each record in file
    @parm int | inRecNum | Index of record to delete
    @rdesc Returns VS_SUCCESS or VS_ERR
    @end
--------------------------------------------------------------------------*/

int inDeleteConfRec(char* szFileName,int inRecSize,int inRecNum) {
    FHANDLE hConfFHandle;

    hConfFHandle = open(szFileName, O_RDWR);

    if (lseek(hConfFHandle, (long) GEN_VER_SIZE + (inRecSize * inRecNum), SEEK_SET) == -1) {
        close(hConfFHandle);
        return(VS_ERR);
    }

    if (delete(hConfFHandle, inRecSize) < 0)   /* delete error */ {
        close(hConfFHandle);
        return(VS_ERR);
    }

    close(hConfFHandle);
    return(VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    @func int | inSearchConfRec | Searches contents of specified record to file
    @parm char * | szFileName | Name of file to save to
    @parm int | inRecSize | Size of each record in file
    @parm int *| inRecNum | Pointer to index of record compared
    @parm char * | pchConfRec | Pointer to location to save the record data to
    @parm char * |  pchComprec | Pointer to record to be used in comparison function
    @rdesc Returns VS_SUCCESS, VS_ERR, or VS_ESCAPE
    @end
--------------------------------------------------------------------------*/

int inSearchConfRec(char* szFileName,int inRecSize,int* inRecNum,char* pchConfRec,int (*inCompare) (char *, char*),char* pchCompRec) {
    FHANDLE hConfFHandle;
    int inResult;

    hConfFHandle = open(szFileName, O_RDWR);

    if (lseek(hConfFHandle, (long) GEN_VER_SIZE, SEEK_SET) == -1) {
        close(hConfFHandle);
        return(VS_ERR);
    }

    *inRecNum = 0;
    while (read(hConfFHandle, pchConfRec, inRecSize) == inRecSize) {
        inResult = (*inCompare) (pchConfRec, pchCompRec);
        if (inResult == VS_ESCAPE) {
            close(hConfFHandle);
            return(VS_ESCAPE);
        } else if (inResult) {
            close(hConfFHandle);
            return(VS_SUCCESS);
        }
        (*inRecNum)++;
    }
    close(hConfFHandle);
    return(VS_ERR);
}


